import sys
import os
from PyQt5.QtWidgets import (QWidget, QLabel,
                             QLineEdit, QApplication,QComboBox)
                             
from PyQt5.QtGui import QPixmap
from PyQt5 import QtCore
class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initData()
        self.initUI()
    def initData(self):
        foodLsit=sorted(os.listdir("data/"))
        self.foodList=foodLsit
    def initUI(self):
        qle = QLineEdit(self)
        qle.move(10,10)
        self.resize(600,990)
        self.setWindowTitle('分类小助手')
        
        self.label=[QLabel(self),QLabel(self),QLabel(self),QLabel(self),QLabel(self)]
        self.combo = QComboBox(self)
        self.combo.move(100, 10)
        self.combo.activated[str].connect(self.onActivated)
        qle.textChanged[str].connect(self.onChanged)
        for i in range(len(self.label)):
            if i<2:
                self.label[i].move(300*i+1,90)
            if i>=2 and i<4:
                self.label[i].move(300*(i-2)+1,390)
            if i==4:
                self.label[i].move(0,690)
        self.show()
        

    def onActivated(self,text):
        basePath="data/foodEX/"+text+"/"
        imgName=os.listdir(basePath)
       
        for i in range(min(len(imgName),len(self.label))):
            
            pixmap = QPixmap(basePath+imgName[i])
            pixmap = pixmap.scaled(300, 300, QtCore.Qt.KeepAspectRatio)
            self.label[i].setPixmap(pixmap)
            self.label[i].adjustSize()
        
    def onChanged(self,text):
        self.combo.clear()
        for food in self.foodList:
            if FuzzySearch(text,food):
                self.combo.addItem(food)

def FuzzySearch(a,b):
    lenA,lenB=len(a),len(b)
    for i in range(lenB-lenA+1):
        if a==b[i:i+lenA]:
            return True
    return False

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())